﻿using DataServer.Entities;
using DataServer.MySql;
using System;
using System.Collections.Generic;
using System.Data;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;

namespace IsPay.Logic
{
    public class UserSer
    {
        public UserProfile CheckUser(String struserid,String strpwd)
        {
            SystemMag sm = new SystemMag();

           UserProfile user= sm.GetUserbyUseridPwd(struserid, strpwd);
           if (user == null || user.UserId==null||user.Pwd==null)
            {
                return null;
            }else
            {
                return user;
            }

        }
        public Boolean CheckUserDouble(String columnname,String strvalue)
        {
            Boolean isdouble = false;
            try
            {
                SystemMag sm = new SystemMag();

                UserProfile user = sm.GetUserbyColumnName(columnname, strvalue);
                if (user == null || user.UserId == null || user.Pwd == null)
                {
                    isdouble = false;
                }
                else
                {
                    isdouble = true;
                }

            }
            catch(Exception e)
            {
                isdouble = false;
            }



            return isdouble;
        }
        public Boolean CheckUserDouble(String columnname, String strvalue,String userid)
        {
            Boolean isdouble = false;
            try
            {
                SystemMag sm = new SystemMag();

                UserProfile user = sm.GetUserbyColumnName(columnname, strvalue, userid);
                if (user == null || user.UserId == null || user.Pwd == null)
                {
                    isdouble = false;
                }
                else
                {
                    isdouble = true;
                }

            }
            catch (Exception e)
            {
                isdouble = false;
            }



            return isdouble;
        }

        public Boolean AddUser(UserProfile user)
        {
            Boolean isadd = true;
            SystemMag sm = new SystemMag();
            isadd = sm.AddUser(user);


            return isadd;
        }
        public Boolean AddUserLoginHis(UserHistory userhis)
        {
            Boolean isadd = true;
            SystemMag sm = new SystemMag();
            isadd = sm.AddUserHis(userhis);
            return isadd;
        }
        public IEnumerable<UserProfile> QueryUser(String userid,String username,String isactive,String usertype)
        {
            IEnumerable<UserProfile> lsuser = new List<UserProfile>();
            SystemMag sm = new SystemMag();

            lsuser = sm.QueryUser(userid, username, isactive, usertype);

            return lsuser;
        }

        public String ModifyUsers(List<UserProfile> lsuser,String userid)
        {
            String strreturnmsg = "OK";
            foreach(UserProfile user in lsuser)
            {
                if (user.IsActive == "1")
                {
                    String strrs = CheckUserBeforeUpdate(user);
                    if (strrs.Equals("OK"))
                    {

                    }
                    else
                    {
                        return strrs;
                    }
                }
            }
            SystemMag symg = new SystemMag();
            if(symg.ModifyUsers(lsuser, userid))
            {

            }else
            {
                strreturnmsg = "更新失败!";
            }


            return strreturnmsg;
        }

        public String CheckUserBeforeUpdate(UserProfile user)
        {
            Regex r = new Regex("^\\s*([A-Za-z0-9_-]+(\\.\\w+)*@(\\w+\\.)+\\w{2,5})\\s*$");
            if (r.IsMatch(user.Email))
            {
                if (CheckUserDouble("Email", user.Email, user.UserId)){
                    return "邮箱已经存在<" + user.Email + ">";
                }else
                {

                }
            }
            else
            {
               
                return "邮箱格式错误";
            }


            r = new Regex("^\\d{11}$");
            if (r.IsMatch(user.Telephone))
            {

                if (CheckUserDouble("Telephone", user.Telephone, user.UserId))
                {
                    return "电话已经存在<" + user.Telephone + ">";
                }
                else
                {

                }
            }
            else
            {
               
                return "电话格式错误";
            }
            if (user.Pwd!=null&&user.Pwd.Length > 0)
            {
                //byte[] result = Encoding.Default.GetBytes(user.Pwd);
                //MD5 md5 = new MD5CryptoServiceProvider();
                //byte[] output = md5.ComputeHash(result);
                //String strMD5pwd = BitConverter.ToString(output).Replace("-", "");
                //user.Pwd = strMD5pwd;
                //user.Pwd = CodeMange.getMD5StringCode(user.Pwd);
            }
            else
            {
                user.Pwd = "";
            }
            return "OK";
        }

        public IEnumerable<UserProfile> GetUsersByAccuntID(String accuntid)
        {
            IEnumerable<UserProfile> lsuser = new List<UserProfile>();
            SystemMag sm = new SystemMag();

            lsuser = sm.GetUsersByAccuntID(accuntid);

            return lsuser;
        }
        public IEnumerable<UserProfile> GetUsersNotSignAccunt()
        {
            IEnumerable<UserProfile> lsuser = new List<UserProfile>();
            SystemMag sm = new SystemMag();

            lsuser = sm.GetUsersNotSignAccunt();

            return lsuser;
        }

        public UserProfile GetUserByUserID(String userid)
        {
            SystemMag sm = new SystemMag();

            return sm.GetUserbyUserid(userid);
        }

        public String UpdateUserAccunt(List<UserProfile> lsadd, List<UserProfile> lsdel, String userid)
        {
            String strreturnmsg = "OK";
            foreach (UserProfile user in lsadd)
            {
                UserProfile us = GetUserByUserID(user.UserId);
                if (us == null || us.UserId.Length <= 0)
                {
                    return "用户ID "+ user.UserId + "  无法找到";
                }else if(us.AccuntId!=null&&us.AccuntId.Length>0)
                {
                    return "用户ID " + user.UserId + "  已经被绑定无法再绑定";
                }
            }
            SystemMag symg = new SystemMag();
            if (symg.UpdateUserAccunt(lsadd, lsdel, userid))
            {

            }
            else
            {
                strreturnmsg = "更新失败!";
            }


            return strreturnmsg;
        }

    }
}